package com.example.ViewPagerSum;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.example.ViewPagerSum.adapter.PhotoDetailAdapter;
import com.example.ViewPagerSum.domain.MMDetailPthoto;
import com.example.ViewPagerSum.domain.MmPhoto;
import com.example.ViewPagerSum.util.*;

import java.util.List;

/**
 * ============================================================
 * author     王景
 * <p/>
 * time       2015/9/14  1:16.
 * <p/>
 * email:     wjontheway@163.com
 * <p/>
 * description
 * <p/>
 * ============================================================
 */

public class PhotoDetailActivity extends Activity implements ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    private String requestPath;
    private TextView title;
    private TextView count;
    private final int SUCCESS = 1;
    private final int FAILED = 2;
    private MenuItem item;
    private MMDetailPthoto mMMDetailPthoto = null;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            switch (what) {
                case SUCCESS:
                    mMMDetailPthoto = (MMDetailPthoto) msg.obj;
                    PhotoDetailAdapter adapter = new PhotoDetailAdapter(mMMDetailPthoto, PhotoDetailActivity.this);
                    viewPager.setAdapter(adapter);
                    title.setText(mMMDetailPthoto.getTitle());
                    item.setTitle((1) + "/" + mMMDetailPthoto.getSize());
                    //circle_progressbar.setVisibility(View.INVISIBLE);
                    break;
                case FAILED:
                    Toast.makeText(PhotoDetailActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_detail);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        title = (TextView) findViewById(R.id.title);
        count = (TextView) findViewById(R.id.count);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        MmPhoto mmPhoto = (MmPhoto) bundle.getSerializable("mmPhoto");
        int id = mmPhoto.getId();
        requestMmPhotoData(id);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);

        //ViewPager左右滑动，触发RadioButton的选中
        viewPager.addOnPageChangeListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.mmdetail,menu);
        item = menu.findItem(R.id.count);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case android.R.id.home:
                finish();
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 请求网络
     */
    public void requestMmPhotoData(int id) {
        // circle_progressbar.setVisibility(View.VISIBLE);
        requestPath = URLTools.IMAGRURL_DETAIL + "?id=" + id;
        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                String json = HttpUtil.getPhotoMmJson(requestPath);
                ParseMmDetailJson pmjd = new ParseMmDetailJson();
                MMDetailPthoto mmPhotoList = pmjd.getMmPhotoList(json);
                Message msg = Message.obtain();
                if (mmPhotoList != null) {
                    msg.obj = mmPhotoList;
                    msg.what = SUCCESS;
                } else {
                    msg.what = FAILED;
                }
                mHandler.sendMessage(msg);
                Looper.loop();
            }
        }).start();
    }


    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        title.setText(mMMDetailPthoto.getTitle());
        item.setTitle((position + 1) + "/" + mMMDetailPthoto.getSize());
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}